package ttOrder.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ttOrder.dao.inter.OrderDetailMapper;
import ttOrder.model.vo.OrderDetailVO;
import ttOrder.model.vo.OrderVO;
import ttOrder.service.OrderDetailServiceImpl;
import ttOrder.service.OrderServiceImpl;
import ttOrder.util.CommonMessageResultVO;

@Controller
@RequestMapping("/ttOrder")
public class OrderController {

	@Resource
    private OrderServiceImpl orderService;
	@Resource
	private OrderDetailServiceImpl orderDetailService;
	
	/**前台获取订单主体列表及其订单详情列表 GET/POST*/
	@RequestMapping("/orders")
	public ModelAndView orders(Model model){
		List<OrderVO> orders = orderService.getAllVO();
		model.addAttribute("orders", orders);
		return new ModelAndView("orders");
	}
	
	/**获取未付订单主体列表及其订单详情列表 POST*/
	@ResponseBody
	@RequestMapping(value = "/ordering", method = RequestMethod.POST)
	public List<OrderVO> orderingList(Model model){
		return orderService.getVOListByStatus(0);
	}
	
	/**获取已付订单主体列表及其订单详情列表 POST*/
	@ResponseBody
	@RequestMapping(value = "/ordered", method = RequestMethod.POST)
	public List<OrderVO> orderedList(Model model){
		return orderService.getVOListByStatus(1);
	}
	
	/**获取订单主体列表及其订单详情列表 POST*/
	@ResponseBody
	@RequestMapping(value = "/orderList", method = RequestMethod.POST)
	public List<OrderVO> orderList(Model model){
		model.addAttribute("orders", "");
		return orderService.getAllVO();
	}
	
	/**获取订单详细列表 POST
	 * @param */
	@ResponseBody
	@RequestMapping( value="/orderDetail", method = RequestMethod.POST )
	public List<OrderDetailVO> getOrderDetail(Model model, int orderId){
		return (orderService.getByKey(new OrderVO(orderId)).getOrderDetailList());
	}

	/**获取订单 POST*/
	@ResponseBody
	@RequestMapping( value="/getOrder", method = RequestMethod.POST )
	public OrderVO getOrder(Model model, int orderId){
		return orderService.getByKey(new OrderVO(orderId));
	}
	
	/**获取订单 POST*/
	@ResponseBody
	@RequestMapping( value="/getOrderByTableId", method = RequestMethod.POST )
	public OrderVO getOrderByTableId(Model model, int tableId){
		return orderService.getVOByTableIdAndStatus(new OrderVO(tableId,0));
	}
	
	/**获取总收入 POST*/
	@ResponseBody
	@RequestMapping( value="/getTotal", method = RequestMethod.POST )
	public double getTotal(Model model){
		 List<OrderVO> vo = orderService.getAllVO();
		 double TotalPrice = 0;
		 for( OrderVO order : vo ){
			 TotalPrice = TotalPrice + order.getTotalPrice();
		 }
		 return TotalPrice;
	}
	
	/**查询订单详细信息 POST*/
	@ResponseBody
	@RequestMapping( value="/findOrder", method = RequestMethod.POST )
	public CommonMessageResultVO findOrder(Model model,int orderId){
		
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		
		try{
			OrderVO key = new OrderVO();
			key.setId(orderId);
			OrderVO vo = orderService.getByKey(key);
			if(vo == null){
				return result;
			}
			List<OrderDetailVO> list = vo.getOrderDetailList();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("orderDetails", list);
			map.put("totalPrice", vo.getTotalPrice());
			result.setMessageMap(map);
			result.setSuccess(true);
		}catch(Exception e){
			e.getStackTrace();
			result.setSuccess(false);
		}
		return result;
	}
}
